package jsyf.oa.dao.wreport;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.oa.entity.wreport.WReportMainEntity;
import jsyf.oa.entity.wreport.WReportUnfinishMainEntity;
import jsyf.oa.entity.wreport.WReportUnfinishPersonEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 未关闭事项抄送人和参与人
 * @author: wangmin
 * @creat: on 2018-9-17
 * @since: V1.0.0
 * @see jsyf.oa.web
 */
@Repository
public class WReportUnfinishPersonDao extends BaseDao {
    public WReportUnfinishPersonDao() {
        super.iBaseEntity = new WReportUnfinishMainEntity();
    }

    public Integer add(WReportUnfinishPersonEntity model) {
        return super.insert("T_WRT_UNFINISH_PERSON", model.getJSONObject());
    }

    public List<JSONObject> getPersonList(String mainId) {
        StringBuffer sbSql = new StringBuffer();
        sbSql.append("select B.NAME USER_NAME,A.* from T_WRT_UNFINISH_PERSON A ");
        sbSql.append("LEFT JOIN T_SYS_USER B ON A.PERSON_ID=B.ID ");
        sbSql.append("WHERE A.MAIN_ID in (" + mainId + ")");

        return queryForJsonList(sbSql.toString());
    }

    public List<JSONObject> getUserListByIds(String userIds) {
        StringBuilder sbIds = new StringBuilder();
        for (String userId : userIds.split("\\|")) {
            sbIds.append("'").append(userId).append("',");
        }
        sbIds.setLength(sbIds.length() - 1);

        if (sbIds.length() > 0) {
            StringBuffer sbSql = new StringBuffer();
            sbSql.append("select ID,NAME from t_sys_user where id in (" + sbIds.toString() + ")");
            return queryForJsonList(sbSql.toString());
        }

        return null;
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, WReportUnfinishMainEntity.class);
    }

}
